﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Localisation;
using RIOWCFSERVICE = Rio.Framework.WCFService.RioWcfServices;

namespace Rio.Framework.Pages.Controls
{
    /// <summary>
    /// Control : Represent OcrMask selection
    /// </summary>
    public partial class OCRThumbBox : UserControl
    {
        #region Private
        private RIOWCFSERVICE.ImageRio m_aImgRio;       // Selected Thumbnail

        private List<OcrMaskBox> m_oOcrMaskList;        // OcrMask to select
        #endregion

        #region Init/Constructor
        public OCRThumbBox()
        {
            InitializeComponent();
        }

        public OCRThumbBox(RIOWCFSERVICE.ImageRio P_aThumbImg, List<OcrMask> P_OcrMaskList)
            :this()
        {
            m_aImgRio = P_aThumbImg;

            m_oOcrMaskList = new List<OcrMaskBox>();
            foreach (OcrMask L_oOcrMask in P_OcrMaskList)
            {
                m_oOcrMaskList.Add(new OcrMaskBox(L_oOcrMask, false));
            }

            // Databinding 
            NameVignette.DataContext = m_aImgRio;
            NameVignette.Loaded += new RoutedEventHandler(NameVignette_Loaded);
            ImageVignette.DataContext = m_aImgRio;
            DynamicCheckBox.ItemsSource = m_oOcrMaskList;

            CheckAll.SetValue(ToolTipService.ToolTipProperty, Resx.GetLabel(ResxListVignetteSearch.SELECTALL));
        }

        void NameVignette_Loaded(object sender, RoutedEventArgs e)
        {
            NameVignette.Text = System.IO.Path.GetFileNameWithoutExtension(NameVignette.Text);
        }
        #endregion

        #region Function

        /// <summary>
        /// Get thumbnail name
        /// </summary>
        /// <returns>thumbnail name</returns>
        public string GetImageRioName()
        {
            return System.IO.Path.GetFileNameWithoutExtension(m_aImgRio.Name);
        }

        #region Check/Uncheck Event
        private void CheckAll_Checked(object sender, RoutedEventArgs e)
        {
            foreach (OcrMaskBox L_oOcrMaskBox in DynamicCheckBox.ItemsSource)
            {
                L_oOcrMaskBox.IsChecked = true;
            }
        }

        private void CheckAll_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (OcrMaskBox L_oOcrMaskBox in DynamicCheckBox.ItemsSource)
            {
                L_oOcrMaskBox.IsChecked = false;
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            int L_iCount = ((List<OcrMaskBox>)DynamicCheckBox.ItemsSource).Where(x => x.IsChecked == true).Count();
            if (L_iCount + 1 == DynamicCheckBox.Items.Count)
            {
                CheckAll.IsChecked = true;
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckAll.Unchecked -= CheckAll_Unchecked;
            CheckAll.IsChecked = false;
            CheckAll.Unchecked += CheckAll_Unchecked;
        }
        #endregion
        #endregion
    }

    // Use for Databinding
    public class OcrMaskBox : INotifyPropertyChanged
    {
        public OcrMask OcrMaskItem { get; set; }
        private bool m_IsChecked;
        public bool IsChecked
        {
            get
            {
                return m_IsChecked;
            }
            set
            {
                m_IsChecked = value;
                OnNotifyPropertyChanged("IsChecked");
            }
        }

        public OcrMaskBox()
        {

        }

        public OcrMaskBox(OcrMask P_oOcrMask, bool P_bIsChecked)
        {
            OcrMaskItem = P_oOcrMask;
            IsChecked = P_bIsChecked;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnNotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
